﻿using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.Setup;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmFunctionSetup : FrmFunctionWithTool
    {
        public FrmFunctionSetup()
        {
            InitializeComponent();
        }


        private void ChoiceEmp()
        {
            if (this.dtEmpAll == null)
            {
                this.dtEmpAll = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT EMP_ID ,EMP_CODE ,EMP_NAME FROM MXH_EMPLOYEE ORDER BY EMP_CODE", ref this.dtEmpAll);
                this.dtEmpAll.PrimaryKey = new DataColumn[] { this.dtEmpAll.Columns["EMP_CODE"] };
            }
            int[] numArray = new int[3];
            numArray[1] = 30;
            numArray[2] = 100;
            FrmMulSelFromGrid grid = new FrmMulSelFromGrid(this.dtEmpAll, new string[] { "标识", "代码", "姓名" }, numArray, "人员选择", ref this.dtEmp, new FrmMulSelFromGrid.SaveData(this.SaveEmp));
            if (grid.ShowDialog(this) == DialogResult.OK)
            {
                this.xtraTabControl1.SelectedTabPage = this.xtraTabPage2;
            }
        }

        private void ChoiceRole()
        {
            if (this.dtRoleAll == null)
            {
                this.dtRoleAll = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT ROLE_CODE ,ROLE_NAME FROM MXHLY_SYS_ROLE ORDER BY ROLE_CODE", ref this.dtRoleAll);
                this.dtRoleAll.PrimaryKey = new DataColumn[] { this.dtRoleAll.Columns["ROLE_CODE"] };
            }
            int[] numArray = new int[2];
            numArray[1] = 100;
            FrmMulSelFromGrid grid = new FrmMulSelFromGrid(this.dtRoleAll, new string[] { "代码", "名称" }, numArray, "角色选择", ref this.dtRole, new FrmMulSelFromGrid.SaveData(this.SaveRole));
            if (grid.ShowDialog(this) == DialogResult.OK)
            {
                this.xtraTabControl1.SelectedTabPage = this.xtraTabPage1;
            }
        }

        private void DeleteNode()
        {
            TreeListNode focusedNode = this.tvFunction.FocusedNode;
            if ((focusedNode != null) && (focusedNode.ImageIndex != 0))
            {
                if (focusedNode.Nodes.Count > 0)
                {
                    XtraMessageBox.Show("请先删除功能子节点! ", "提示");
                }
                else
                {
                    string paramValue = focusedNode.GetValue("CODE").ToString();
                    string str2 = focusedNode.GetValue("NAME").ToString();
                    if (XtraMessageBox.Show("是否真的删除" + paramValue + str2 + "?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (focusedNode.ImageIndex == 1)
                        {
                            string commandtext = "DELETE FROM MXHLY_SYS_MODULE WHERE MODULE_CODE = :module_code";
                            if (SystemInfo.DbData.ExecuteSql(commandtext, new DataParamCol(new DataParam[] { new InputDataParam("module_code", paramValue) })) >= 0)
                            {
                                this.tvFunction.Nodes.Remove(focusedNode);
                            }
                            else
                            {
                                XtraMessageBox.Show("删除模块不成功!\n" + SystemInfo.DbData.ErrorMessage);
                            }
                        }
                        else
                        {
                            DataParamCol[] colArray;
                            string[] commandtexts = new string[4];
                            colArray = new DataParamCol[] { new DataParamCol(new DataParam[] { new InputDataParam("function_code", paramValue) }), new DataParamCol(new DataParam[] { new InputDataParam("function_code", paramValue) }), new DataParamCol(new DataParam[] { new InputDataParam("function_code", paramValue) }), new DataParamCol(new DataParam[] { new InputDataParam("function_code", paramValue) }) };
                            commandtexts[0] = "DELETE FROM MXHLY_SYS_FUNCTION WHERE FUNCTION_CODE = :function_code";
                            commandtexts[1] = "DELETE FROM MXHLY_SYS_FUNCTION_ROLE WHERE FUNCTION_CODE = :function_code";
                            commandtexts[2] = "DELETE FROM MXHLY_SYS_FUNCTION_EMP WHERE FUNCTION_CODE = :function_code";
                            commandtexts[3] = "DELETE FROM MXHLY_SYS_MENU WHERE FUNCTION_CODE = :function_code";
                            if (SystemInfo.DbData.RunSql(commandtexts, colArray) == 0)
                            {
                                this.tvFunction.Nodes.Remove(focusedNode);
                            }
                            else
                            {
                                XtraMessageBox.Show("删除功能不成功!\n" + SystemInfo.DbData.ErrorMessage);
                            }
                        }
                    }
                }
            }
        }

        private void EditNode()
        {
            TreeListNode focusedNode = this.tvFunction.FocusedNode;
            if (focusedNode.ImageIndex != 0)
            {
                if (focusedNode.ImageIndex == 2)
                {
                    focusedNode.GetValue(this.tlcCode).ToString();
                    FrmFunctionEdit edit = new FrmFunctionEdit();
                    edit.SetParameter(focusedNode, false);
                    edit.ShowDialog(this);
                }
                else
                {
                    FrmModuleEdit edit2 = new FrmModuleEdit();
                    edit2.SetParameter(focusedNode);
                    edit2.ShowDialog(this);
                }
            }
        }

        private void FreshNode(int index, bool isused)
        {
            this.nbiDelete.Visible = index >= 1;
            this.nbiEdit.Visible = index >= 1;
            this.nbiRole.Visible = (index == 2) && isused;
            this.nbiEmp.Visible = (index == 2) && isused;
            this.splitContainerControl1.Panel2.Visible = (index == 2) && isused;
        }

        private void FreshRoleAndEmp(string function_code)
        {
            string commandtext = "SELECT F.ROLE_CODE, R.ROLE_NAME \r\n   FROM MXHLY_SYS_FUNCTION_ROLE F, MXHLY_SYS_ROLE R \r\n   WHERE F.ROLE_CODE = R.ROLE_CODE AND F.FUNCTION_CODE = :function_code \r\n   ORDER BY F.ROLE_CODE ASC";
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("function_code", function_code) });
            this.dtRole.Rows.Clear();
            SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtRole);
            this.dtRole.PrimaryKey = new DataColumn[] { this.dtRole.Columns["ROLE_CODE"] };
            this.gvRole.GridControl.DataSource = this.dtRole;
            string str2 = "SELECT F.EMP_ID, E.EMP_CODE ,E.EMP_NAME \r\n   FROM MXHLY_SYS_FUNCTION_EMP F, MXH_EMPLOYEE E \r\n   WHERE F.EMP_ID = E.EMP_ID AND F.FUNCTION_CODE = :function_code \r\n   ORDER BY E.EMP_CODE ASC";
            this.dtEmp.Rows.Clear();
            SystemInfo.DbData.Retrieve(str2, parm, ref this.dtEmp);
            this.dtEmp.PrimaryKey = new DataColumn[] { this.dtEmp.Columns["EMP_CODE"] };
            this.gvEmp.GridControl.DataSource = this.dtEmp;
        }

        private void gvEmp_DoubleClick(object sender, EventArgs e)
        {
            this.ChoiceEmp();
        }

        private void gvRole_DoubleClick(object sender, EventArgs e)
        {
            this.ChoiceRole();
        }

        public override bool Init()
        {
            bool flag = false;
            if (base.Init())
            {
                try
                {
                    this.dtRole = new DataTable();
                    this.gvRole.GridControl.DataSource = this.dtRole;
                    this.dtEmp = new DataTable();
                    this.gvEmp.GridControl.DataSource = this.dtEmp;
                    DataTable dt = new DataTable();
                    string commandtext = "SELECT MODULE_CODE ,MODULE_NAME ,MODULE_FILENAME ,'' ,USED_FLAG FROM MXHLY_SYS_MODULE ORDER BY MODULE_CODE";
                    SystemInfo.DbData.Retrieve(commandtext, ref dt);
                    string str2 = "SELECT FUNCTION_CODE ,FUNCTION_NAME ,OBJECT_NAME ,PARAMETER ,USED_FLAG ,DESCRIBE \r\n    FROM MXHLY_SYS_FUNCTION \r\n    WHERE MODULE_CODE = :module_code \r\n    ORDER BY FUNCTION_CODE ASC ";
                    this.tvFunction.BeginUpdate();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string paramValue = dt.Rows[i]["MODULE_CODE"].ToString();
                        TreeListNode parentNode = this.tvFunction.AppendNode(dt.Rows[i], this.tvFunction.Nodes[0]);
                        parentNode.SelectImageIndex = 1;
                        parentNode.ImageIndex = 1;
                        DataTable table2 = new DataTable();
                        DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("module_code", paramValue) });
                        SystemInfo.DbData.Retrieve(str2, parm, ref table2);
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            TreeListNode node2 = this.tvFunction.AppendNode(table2.Rows[j], parentNode);
                            node2.SelectImageIndex = 2;
                            node2.ImageIndex = 2;
                        }
                    }
                    Cursor.Current = Cursors.Default;
                    this.tvFunction.EndUpdate();
                    this.tvFunction.Nodes[0].Expanded = true;
                    //base.ZoomWin();
                    this.FreshNode(0, true);
                    flag = true;
                }
                catch (Exception exception)
                {
                    XtraMessageBox.Show("初始化对象不成功!\n" + exception.Message, "出错");
                }
            }
            return flag;
        }

        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.DeleteNode();
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.EditNode();
        }

        private void nbiEmp_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.xtraTabControl1.SelectedTabPage = this.xtraTabPage2;
            this.ChoiceEmp();
        }

        private void nbiExit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.NewNode();
        }

        private void nbiPrint_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;
            //if (PrintHelper.IsPrintingAvailable)
            //{
            //    PrintHelper.ShowPreview(this.tvFunction);
            //}
            //else
            //{
            //    XtraMessageBox.Show("打印库文件未找到！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //}
            Cursor.Current = Cursors.Default;
        }

        private void nbiRole_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.xtraTabControl1.SelectedTabPage = this.xtraTabPage1;
            this.ChoiceRole();
        }

        private void NewNode()
        {
            TreeListNode focusedNode = this.tvFunction.FocusedNode;
            if (focusedNode != null)
            {
                if (focusedNode.ImageIndex == 2)
                {
                    focusedNode = focusedNode.ParentNode;
                }
                if (focusedNode.ImageIndex == 1)
                {
                    focusedNode.GetValue(this.tlcCode).ToString();
                    FrmFunctionEdit edit = new FrmFunctionEdit();
                    edit.SetParameter(focusedNode, true);
                    edit.ShowDialog(this);
                }
                else
                {
                    FrmModuleAdd add = new FrmModuleAdd();
                    add.SetParameter(focusedNode);
                    add.ShowDialog(this);
                }
            }
        }

        private bool SaveEmp(DataTable dt)
        {
            dt.AcceptChanges();
            string[] commandtexts = new string[dt.Rows.Count + 1];
            DataParamCol[] parms = new DataParamCol[dt.Rows.Count + 1];
            string paramValue = this.tvFunction.FocusedNode.GetValue("CODE").ToString();
            commandtexts[0] = "DELETE FROM MXHLY_SYS_FUNCTION_EMP WHERE FUNCTION_CODE = :function_code";
            parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("function_code", paramValue) });
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                commandtexts[i + 1] = "INSERT INTO MXHLY_SYS_FUNCTION_EMP (FUNCTION_CODE ,EMP_ID) VALUES (:fcode ,:empid)";
                parms[i + 1] = new DataParamCol(new DataParam[] { new InputDataParam("fcode", paramValue), new InputDataParam("empid", dt.Rows[i]["EMP_ID"]) });
            }
            if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
            {
                return true;
            }
            XtraMessageBox.Show("保存人员不成功！\r\n" + SystemInfo.DbData.ErrorMessage, "出错");
            return false;
        }

        private bool SaveRole(DataTable dt)
        {
            dt.AcceptChanges();
            string[] commandtexts = new string[dt.Rows.Count + 1];
            DataParamCol[] parms = new DataParamCol[dt.Rows.Count + 1];
            string paramValue = this.tvFunction.FocusedNode.GetValue("CODE").ToString();
            commandtexts[0] = "DELETE FROM MXHLY_SYS_FUNCTION_ROLE WHERE FUNCTION_CODE = :function_code";
            parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("function_code", paramValue) });
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                commandtexts[i + 1] = "INSERT INTO MXHLY_SYS_FUNCTION_ROLE (FUNCTION_CODE ,ROLE_CODE) VALUES (:fcode ,:rcode)";
                parms[i + 1] = new DataParamCol(new DataParam[] { new InputDataParam("fcode", paramValue), new InputDataParam("rcode", dt.Rows[i]["ROLE_CODE"].ToString()) });
            }
            if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
            {
                return true;
            }
            XtraMessageBox.Show("保存角色不成功！\r\n" + SystemInfo.DbData.ErrorMessage, "出错");
            return false;
        }

        private void tvFunction_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            if (e.Node.ImageIndex == 0)
            {
                this.FreshNode(0, true);
            }
            else
            {
                bool isused = e.Node.GetValue(this.tlcUserFlag).ToString() == "Y";
                if ((e.Node.ImageIndex == 2) && isused)
                {
                    this.FreshRoleAndEmp(e.Node.GetValue(this.tlcCode).ToString());
                }
                this.FreshNode(e.Node.ImageIndex, isused);
            }
        }
    }
}
